home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14355 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  90 lines

  1. Path: ix.netcom.com!news
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Derivation and calling virtual functions
  5. Date: 29 Mar 1996 17:54:09 GMT
  6. Organization: Netcom
  7. Message-ID: <4jh841$1gp@dfw-ixnews6.ix.netcom.com>
  8. References: <graphix.828032689@spiff.cc.iastate.edu>
  9. NNTP-Posting-Host: den-co10-17.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Fri Mar 29 11:54:09 AM CST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <graphix.828032689@spiff.cc.iastate.edu>, graphix@iastate.edu 
  16. says...
  17. >
  18. >  Say we have the following:
  19. >
  20. >class Base {
  21. >public:
  22. >  virtual void func() { // some stuff }
  23. >  virtual void write() { func(); };
  24. >};
  25. >
  26. >class Derived : public Base {
  27. >public:
  28. >  void func() { // some stuff }
  29. >  write(); { Base::func(); }
  30. >};
  31. >Derived inst;
  32.  
  33.  
  34. First of all, let's write this so that it works:
  35.  
  36. class Base {
  37. public:
  38.   virtual void func() { /*some stuff*/ }
  39.   virtual void write() { func(); };
  40. };
  41.  
  42. class Derived : public Base {
  43. public:
  44.   void func() { /* some stuff */ }
  45.   // need "void" before "write" to define the same method signature
  46.   // According to the ARM 10.2 p.208 "It is an error for a derived 
  47.   // class function to differ from a base class' virtual function
  48.   // in return type only.
  49.   void write(); { Base::func(); }
  50. };
  51. Derived inst;
  52.  
  53. However, the above changes do not in themselves answer the question,
  54. they only make it possible to explore what is really asked
  55. without confusion.
  56.  
  57. Note that the "virtual" is implicit in the derived method
  58. delcarations and is unnecessary.
  59.  
  60. >  Now, when I call inst.write() it in turn calls Base::write() which in
  61. >turn calls Base::func().  Is there a way to instead have it call
  62. >Derived::func() if the instance is of type Derived?  I hoped the 
  63. >virtual keyword would help in this case.
  64.  
  65. Whoa!!!  Derived::write() *does not* call Base::write()!
  66.  
  67. If we write it the way you wanted it to work:
  68.  
  69.    class Derived : public Base {
  70.    public:
  71.      void func() { printf("derived::func\n"); }
  72.      void write() { Base::write(); }
  73.    };
  74.  
  75.    main()
  76.    {
  77.       Derived inst;
  78.       inst.write();
  79.    }
  80.    
  81. When run, this produces:   
  82.    derived::func
  83.  
  84.  
  85. Guess you forgot to compile with the -DWIM switch   :-)
  86.  
  87. john lilley
  88.  
  89.  
  90.